package com.example.erp.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.erp.config.error.CustomException;
import com.example.erp.entity.Goods;
import com.example.erp.entity.Type;
import com.example.erp.entity.dto.ExcelDto;
import com.example.erp.entity.dto.GoodsDto;
import com.example.erp.generator.CodeGenerator;
import com.example.erp.mapper.GoodsMapper;
import com.example.erp.mapper.TypeMapper;
import com.example.erp.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.erp.service.IInoutBoundService;
import com.example.erp.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 食品 服务实现类
 * </p>
 *
 * @author rws
 * @since 2025-01-08
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {


    @Autowired
    private IInoutBoundService iInoutBoundService;
    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private TypeMapper typeMapper;

    @Override
    public void insertGoods(List<ExcelDto> excelDtoList) {

        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String time = now.format(formatter);


        List<Goods>goodsList=new ArrayList<>();
        for(ExcelDto excelDto:excelDtoList) {
            //名称,单位不都相同
            QueryWrapper<Goods> GoodsWrapper = new QueryWrapper<>();
            GoodsWrapper.allEq(Map.of("goods_name",excelDto.getGoodsName(),"units",excelDto.getUnits()));
            long count=this.count(GoodsWrapper);
            if(count>0){
//                throw new CustomException("数据错误，导入失败");
                continue;
            }

            String code;
            while (true){
                code = CodeGenerator.createCode();
                QueryWrapper<Goods> Wrapper = new QueryWrapper<>();
                Wrapper.eq("goods_code", code);
                Goods goods=goodsMapper.selectOne(Wrapper);
                if(goods==null)break;
            }

//            excelDto.setGoodsCode(code);

            QueryWrapper<Type> TypeWrapper1 = new QueryWrapper<>();
            TypeWrapper1.eq("name",excelDto.getLevel1Name());
            Type type1=typeMapper.selectOne(TypeWrapper1);

            QueryWrapper<Type> TypeWrapper2 = new QueryWrapper<>();
            TypeWrapper2.eq("name",excelDto.getLevel2Name());
            Type type2=typeMapper.selectOne(TypeWrapper2);



            Goods goods=new Goods();
            goods.setGoodsCode(code)
                    .setGoodsName(excelDto.getGoodsName())
                    .setFirstType(type1.getTypeId())
                    .setUnits(excelDto.getUnits())
                    .setPrice(excelDto.getPrice())
                    .setCreateBy(StpUtil.getLoginIdAsInt())
                    .setUpdateBy(StpUtil.getLoginIdAsInt())
                    .setCreateTime(time)
                    .setUpdateTime(time);

            if(type2!=null)goods.setSecondType(type2.getTypeId());

            goodsList.add(goods);
        }

        this.saveBatch(goodsList);
        iInoutBoundService.insertInoutBound(excelDtoList);
    }

    @Override
    public void updatePic(GoodsDto goodsDto) throws IOException {
        String imageUrl = goodsDto.getGoods_pic();
        String savePath = "../resources/pics/";
        URL url = new URL(imageUrl);
        BufferedImage image = ImageIO.read(url);
        File outputFile = new File(savePath);
        ImageIO.write(image, "jpg", outputFile);
    }

}
